/**
 * 应用程序入口
 */
require([
	"jquery", "underscore", "jqueryui", "bootstrap", "bootstrapFormHelper", "bootstrapValidator",
	'config',
	'util/formElementManager',
	"util/dragUtil",
	"util/formRender",
	"util/formView",
	"util/formValidator"
], function($, _, jqueryui, bootstrap, bootstrapFormHelper, bootstrapValidator, config, formElementManager, dragUtil, formRender, formView, formValidator) {

	{
		//全局初始化
		//读取工作区宽度
		let layoutScreenSizeType = function() {
			let w = 0;
			if(typeof PAGE_IS_DESIGN === 'boolean') {
				w = $("#layout-main").width();
			} else if(typeof PAGE_IS_FORM === 'boolean') {
				w = $("#form-wrapper").width();
			} else {
				w = $("body").width();
			}
			if(w == 0) {
				return;
			}
			//超小屏幕 手机 (<768px)	小屏幕 平板 (≥768px)	中等屏幕 桌面显示器 (≥992px)	大屏幕 大桌面显示器 (≥1200px)
			let screenName = 'screen-size-mobile';
			if(w >= 992) {
				screenName = 'screen-size-desktop';
			}
			$("body").removeClass("screen-size-mobile").removeClass("screen-size-desktop").addClass(screenName);
		}
		layoutScreenSizeType();
		$(window).resize(layoutScreenSizeType);

	}

	console.log(APP_CONFIG);

	let _private_loadModelPage = function(onComplete, onSuccess, onError) {
		
		let url = APP_CONFIG.domain.cmdb + "/mock/mockdata.json";// 从模拟接口取数据
		
		commonUtil.mask();
		$.ajax({
			"url": url,
			"type": "get",
			"dataType": "json",
			"success": function(data) {
				console.log(data);
				if(typeof onSuccess === 'function') {
					onSuccess(data);
				}
			},
			"error": function() {
				console.log();
				if(typeof onError === 'function') {
					onError();
				}
			},
			"complete": function() {
				console.log("complete");
				commonUtil.unmask();
				if(typeof onComplete === 'function') {
					onComplete();
				}
			}
		});
	}

	/**
	 * 表单定义入口
	 */
	let mainFormDesign = function() {

		let _page_render = () => {
			//先加载组件信息，再初始化拖拽功能
			//初始化拖拽组件
			dragUtil.init();

			//渲染页面组件
			formRender.render();

		}

		let handelModelProperties = function(list) {
			if(typeof list === 'object' && list.length) {
				for(let i in list) {
					let property = list[i];
					console.log(property);

				}
			} else {
				console.log("无模型字段");
			}
		}
		//加载后台模型字段
		if(typeof PAGE_IS_DESIGN === 'boolean') {
			_private_loadModelPage(function() {
				_page_render();
			}, function(data) {
				if(data.code == 200) {
					let result = data.data;
					//handelModelProperties(data.result.list);
					formElementManager.manageModelProperies(result ?.fields);
					formRender.renderByHtml(result ?.html);
					pageKind = result ?.pageKind;
				} else {
					alert("加载模型字段失败");
				}
			}, function() {
				alert("加载模型字段失败");
			});

		} else {
			_page_render();
		}

	}

	/**
	 * 表单显示入口(表单增删改实际功能)
	 */
	let mainFormView = function() {

		let _page_render = () => {

			//渲染页面组件
			formRender.render();
		}

		//加载后台模型字段
		if(typeof PAGE_IS_FORM === 'boolean') {
			_private_loadModelPage(function() {

			}, function(data) {
				if(data.code == 200) {
					let result = data.result;
					pageKind = result ?.pageKind;
					formElementManager.manageModelProperies(result ?.fields);
					formView.rendPage(result ?.html);
					//formView.renderByHtml(result?.echoElement).renderButton(pageKind);
					_page_render();
				} else {
					alert("加载模型页面失败");
				}
			}, function() {
				alert("加载模型页面失败");
			});

		}
	}

	//初始化组件管理器
	/**
	 *  拖拽表单 和 用于操作模型数据的表单页面，都需要加载组件信息， 关系到提交数据的前缀后缀、验证等，都需要根据组件定义进行渲染
	 */
	let configureElements = APP_CONFIG.formElement;
	let componentList = Object.keys(configureElements);
	require(componentList.map((val) => {
		return "components/" + val;
	}), function() {
		let componentDef = [],
			i, len = arguments.length;
		for(i = 0; i < len; i++) {
			componentDef.push({
				"name": componentList[i],
				"conf": configureElements[componentList[i]],
				"def": arguments[i]
			});
		}
		formElementManager.init(componentDef);

		if(typeof PAGE_IS_DESIGN === 'boolean') {
			mainFormDesign();
		} else if(typeof PAGE_IS_FORM === 'boolean') {
			mainFormView();
		} else {
			dragUtil.init();
			//渲染页面组件
			formRender.render();
		}
		
		//测试
		require(['test']);

	});

});